package com.hqyj.crmdemo.util;

import com.hqyj.crmdemo.entity.User;
import io.jsonwebtoken.*;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

//封装一个类，对对象进行加密，对对象进行解密，判断token是否有效
@Component
public class JWTUtil {


    //定义一个过期时间
    private long exprice = 30*60*1000;
    //定义一个加密的密钥
    private String secret = "nice.to.meet.you";

    //1.生成token
    public String createToken(User user){
        JwtBuilder builder = Jwts.builder();
        Map<String, Object> map = new HashMap<>();
        map.put("user",user);
        builder.setClaims(map);
        //设置主题
        builder.setSubject(user.getuName());
        //设置发布日期 -- iat
        builder.setIssuedAt(new Date());
        //设置过期时间 -- exp
        builder.setExpiration(new Date(System.currentTimeMillis()+ exprice));
        //选择加密算法 -->  SignatureAlgorithm.ES512
        //加密的秘钥 --> secret
        builder.signWith(SignatureAlgorithm.HS512,secret);
        String compact = builder.compact();
        return compact;
    }
    //2。解析
    public Claims parseToken(String token){
        JwtParser parser = Jwts.parser();
        //设置密钥(加密的密钥和解密的密钥相同)
        JwtParser jwtParser = parser.setSigningKey(secret);
        Jws<Claims> claimsJws = parser.parseClaimsJws(token);
        Claims body = claimsJws.getBody();
        return body;
    }

    //3.判断token是否有效
    public Boolean validToken(String token){
        try {
            parseToken(token);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }

    //4.获取myUser对象
    public User getUser(String token){
        try {
            Claims claims = parseToken(token);
            Object obj = claims.get("user");
            Map<String, Object> map = (Map<String,Object>)obj;
            User user = new User();

            if (claims instanceof Map) {
                if(map.get("uId") != null){
                    user.setuId((Integer) map.get("uId"));
                }
                if(map.get("uName") != null){
                    user.setuName((String) map.get("uName"));
                }
                if(map.get("uPassword") != null){
                    user.setuPassword((String) map.get("uPassword"));
                }
                if(map.get("rId") != null){
                    user.setrId((Integer) map.get("rId"));
                }
                return user;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;

    }

    public static void main(String[] args) {
//        com.hqyj.springbootmybatis.Util.JWTUtil jwtUtil = new com.hqyj.springbootmybatis.Util.JWTUtil();
//        MyUser myUser = new MyUser();
//        myUser.setUserName("tom");
//        myUser.setRid(1);
//        String token = jwtUtil.createToken(myUser);
//        System.out.println(token);
//        String str = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ0b20iLCJleHAiOjE2NjQ0MjQ3NDQsInVzZXIiOnsidXNlck5vIjpudWxsLCJ1c2VyTmFtZSI6InRvbSIsInVzZXJQd2QiOm51bGwsInJpZCI6MX0sImlhdCI6MTY2NDQyMjk0NH0.5a8fAGSI5yalXrRsXd-hyUlysxhePXKn3Hk50TILlVzk7YSoD0-mK7fp3tp_GgeqBAKsVH9b_6jGDzUib2YPcw";
//        Claims claims = jwtUtil.parseToken(str);
//        System.out.println(claims);
//
//        MyUser user = jwtUtil.getUser(str);
//        System.out.println(user);



    }
}
